package com.atguigu.gulimail.order.service.impl;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimail.order.dao.OrderItemDao;
import com.atguigu.gulimail.order.entity.OrderItemEntity;
import com.atguigu.gulimail.order.service.OrderItemService;

@RabbitListener(queues = "atguigu")
@Service("orderItemService")
public class OrderItemServiceImpl extends ServiceImpl<OrderItemDao, OrderItemEntity> implements OrderItemService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderItemEntity> page = this.page(
                new Query<OrderItemEntity>().getPage(params),
                new QueryWrapper<OrderItemEntity>()
        );

        return new PageUtils(page);
    }
    //rabbit监听器
    /*
    * 参数可以写以下类型：
    * 1。Message message原生消息详细信息 头+体
    * 2。T<发送消息的类型> String s
    * 3。Channel channel 传输数据的通道
    *
    * Queue:  很多人都来监听.只要收到消息，队列就会删除消息，而且只能有一个收到此消息
    * 比如：服务启动多个，同一个消息只有一个客户端能收到
    * 只有一个消息完全处理完，方法运行结束，才能接收下一个消息
    * */
    @RabbitHandler
    public void receiveMessage(Message message, String s, Channel channel) throws IOException {

        //签收消息，告诉服务器消息已经收到了
        if (message.getMessageProperties().getDeliveryTag()%2==0){
            System.out.println(message.getMessageProperties().getDeliveryTag()+"号消息已签收");
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }else {
            channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);
            System.out.println(message.getMessageProperties().getDeliveryTag()+"号消息已重新入队");
        }

//        System.out.println("接收到的消息:" +message+"类型："+s);

    }


}